import Phaser from 'phaser'
import PlayerBase from "./PlayerBase";
import PlayerTiequan from '../players/PlayerTiequan';
import PlayerHuonv from '../players/PlayerHuonv';

/**
 * 导演类
 * 该类主要负责创建角色并控制其动作间的配合
 */
export default class Director {
    public scene: Phaser.Scene = null!;
    public playerList: PlayerBase[] = [];
    public playerMapping: Object = {};
    public monsterList: PlayerBase[] = [];
    public monsterMapping: Object = {};
    public playerMain: PlayerBase = null!;
    private currentPlayerIndex: number = 0;
    private userId: number = window.userInfo.id;
    public rxCount: number = 0;

    constructor(scene: Phaser.Scene) {
        this.scene = scene;
    }

    selectMainPlayer(playerIndex: number = 0) {
        if (playerIndex >= this.playerList.length) playerIndex = 0;
        this.playerMain = this.playerList[playerIndex];
        this.scene.cameras.main.startFollow(this.playerMain.spine, true, 1, 1, 0, 70);
    }

    switchPlayer() {
        this.currentPlayerIndex++;
        if (this.currentPlayerIndex >= this.playerList.length) this.currentPlayerIndex = 0;
        this.selectMainPlayer(this.currentPlayerIndex);
    }

    setCamerasToPlayer(player: PlayerBase) {
        this.scene.cameras.main.startFollow(player.spine, true, 1, 1, 0, 70);
    }

    createTest(): void {
        this.playerList = [
            new PlayerHuonv(this),
            new PlayerTiequan(this),
        ];

        let i = 0;
        this.playerList.forEach(element => {
            element.setPositionX(1000 + i);
            i = i + 350;
        });
        // setInterval(() => {
        //     this.playerList[1].setActionSkillTwo();
        // }, 1000);
        this.playerMain = this.playerList[0];
    }

    addPlayer(info: any): boolean {
        if (this.playerMapping[info.id] != null) {
            return false;
        }

        console.log(info.role.roleKey)

        let player: PlayerBase = null!;
        if (info.role.roleKey == 'sanim_234051') {
            player = new PlayerTiequan(this);
        }
        else if (info.role.roleKey == 'sanim_224061') {
            player = new PlayerHuonv(this);
        }
        if (player == null) {
            console.log('没有找到对应的角色', info);
            return false;
        }
        player.serverInfo = info;
        /** 加入到字典方便数据更新 */
        this.playerMapping[info.id] = player;
        /** 加入数组方便统一管理 */
        this.playerList.push(player);
        /** 初始化主角角色 */
        if (this.userId == info.userId) {
            this.playerMain = player;
            this.setCamerasToPlayer(player);
        }
        return true;
    }

    addMonster(info: any): boolean {
        if (this.monsterMapping[info.id] != null) {
            return false;
        }
        console.log(info.role.roleKey)
        console.log(info)
        let player: PlayerBase = new PlayerBase(this, info.role.roleKey, true);
        player.serverInfo = info;
        /** 加入到字典方便数据更新 */
        this.monsterMapping[info.id] = player;
        /** 加入数组方便统一管理 */
        this.monsterList.push(player);
        return true;
    }


    update(infos: any) {
        if ('playerList' in infos && infos.playerList != null && infos.playerList.length > 0) {
            infos.playerList.forEach(element => {
                let player: PlayerBase = this.playerMapping[element.id];

                if (player == null) {
                    this.addPlayer(element);
                }
                else {
                    player.serverInfo = element;
                    player.syncNetwork();
                }
                // else if (player != this.playerMain) {
                //     player.serverInfo = element;
                //     player.syncNetwork();
                // }
            });
        }

        if ('monsterList' in infos && infos.monsterList != null && infos.monsterList.length > 0) {
            infos.monsterList.forEach(element => {
                let player: PlayerBase = this.monsterMapping[element.id];
                if (player == null) {
                    this.addMonster(element);
                }
                else {
                    player.serverInfo = element;
                    player.syncNetwork();
                }
            });
        }
    }

    findAttachedPlayers(player: PlayerBase, x: number, y: number, ex: number, ey: number): PlayerBase[] {

        let attachedPlayers: PlayerBase[] = [];
        for (let i = 0; i < this.playerList.length; i++) {
            if (this.playerList[i] == player) {
                continue;
            }
            if (this.playerList[i].spine.x >= x && this.playerList[i].spine.x <= ex &&
                this.playerList[i].spine.y >= y && this.playerList[i].spine.y <= ey) {
                if (this.playerList[i].isDead() == false) {
                    attachedPlayers.push(this.playerList[i]);
                }
            }
        }
        return attachedPlayers;
    }



}